package com.ibiaoke.goods.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.utils.StringUtils;
import com.ibiaoke.goods.entity.GoodStock;
import com.ibiaoke.goods.entity.GoodStockExample;
import com.ibiaoke.goods.mapper.GoodStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;

@Repository
public class GoodStockDao extends BaseDao<GoodStock, GoodStockExample, Integer, GoodStockMapper> {
    @Autowired
    private GoodStockMapper goodStockMapper;
    @Override
    public GoodStockMapper getMapper() {
        return goodStockMapper;
    }


    public List<GoodStock> findGoodStockList(String goodName, Integer id,Integer goodId, Date buyTime,Date buyEndTime,
                                             String supplier,String buyPro,String buyCity,Integer shopId,Page page) {
        GoodStockExample example = new GoodStockExample();
        GoodStockExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotEmpty(goodName)) {
            criteria.andGoodNameLike("%" + goodName + "%");
        }

        if (StringUtils.isNotEmpty(buyPro)) {
            criteria.andBuyProEqualTo(buyPro);
        }

        if (StringUtils.isNotEmpty(buyCity)) {
            criteria.andBuyCityEqualTo(buyCity);
        }

        if(goodId != null){
            criteria.andGoodIdEqualTo(goodId);
        }

        if (id != null) {
            criteria.andIdEqualTo(id);
        }

        if (StringUtils.isNotEmpty(supplier)) {
            criteria.andSupplierLike("%" + supplier + "%");
        }

        if (buyTime != null) {
            criteria.andBuyTimeGreaterThanOrEqualTo(buyTime);
        }

        if(buyEndTime != null){
            criteria.andBuyTimeLessThanOrEqualTo(buyEndTime);
        }

        if(shopId != null){
            criteria.andShopIdEqualTo(shopId);
        }

        page.setTotalCount(goodStockMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("create_time desc");
        return selectByExample(example);
    }

}
